package com.nineclock.system.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.nineclock.common.constant.NcConstant;
import com.nineclock.common.enums.ResponseEnum;
import com.nineclock.common.exception.NcException;
import com.nineclock.common.utils.BeanHelper;
import com.nineclock.system.dto.SysCompanyContactConfigDTO;
import com.nineclock.system.mapper.SysCompanyContactConfigMapper;
import com.nineclock.system.pojo.SysCompanyContactConfig;
import com.nineclock.system.service.SysCompanyContactConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class SysCompanyContactConfigServiceImpl implements SysCompanyContactConfigService {

    @Autowired
    private SysCompanyContactConfigMapper configMapper;

    @Override
    public void addContactConfig(SysCompanyContactConfigDTO configDTO) {

        //校验
        if(configDTO == null || StrUtil.isEmpty(configDTO.getName())){
            throw new NcException(ResponseEnum.INVALID_PARAM_ERROR);
        }

        Long companyId = 1L; //TODO 目前先固定为1 , 后续完善
        //组装数据
        SysCompanyContactConfig config = BeanHelper.copyProperties(configDTO, SysCompanyContactConfig.class);
        config.setCompanyId(companyId);
        config.setStatus((short)1);
        config.setFieldType("string");

        //保存
        configMapper.insert(config);
    }



    @Override
    public List<SysCompanyContactConfigDTO> queryAndInit() {
        List<SysCompanyContactConfigDTO> configList = new ArrayList<>();

        Long companyId = 1L; //TODO 目前先固定为1 , 后续完善

        //封装条件, 查询
        LambdaQueryWrapper<SysCompanyContactConfig> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysCompanyContactConfig::getCompanyId, 1L);//where company_id = 1

//        QueryWrapper<SysCompanyContactConfig> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("company_id", companyId);
        List<SysCompanyContactConfig> configs = configMapper.selectList(queryWrapper);


        if(CollectionUtil.isEmpty(configs)){//初始化
            for (String name : NcConstant.COMPANY_CONFIG_CONTACT_LIST) {
                //数据组装
                SysCompanyContactConfig config = new SysCompanyContactConfig();
                config.setName(name);
                config.setCompanyId(companyId);
                config.setFieldType("string");
                config.setStatus((short)1);
                config.setType("fixed");

                configMapper.insert(config);

                configList.add(BeanHelper.copyProperties(config, SysCompanyContactConfigDTO.class));
            }
            return configList;
        }

        return BeanHelper.copyWithCollection(configs, SysCompanyContactConfigDTO.class);
    }


    @Override
    public void updateStatus(Long id, Short status) {

        //校验
        if(id == null || status == null){
            throw new NcException(ResponseEnum.INVALID_PARAM_ERROR);
        }

        SysCompanyContactConfig config = configMapper.selectById(id);
        if(config == null){
            throw new NcException(ResponseEnum.INVALID_PARAM_ERROR);
        }

        //更新
        config.setStatus(status);
        configMapper.updateById(config);
    }
}
